//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.zj.easywebview

import com.zj.easywebview.util.DisplayUtil.dip2px
import android.annotation.SuppressLint
import android.content.Context
import kotlin.jvm.JvmOverloads
import android.widget.RelativeLayout
import android.graphics.Bitmap
import android.net.http.SslError
import android.os.Build.VERSION
import android.os.Message
import android.util.AttributeSet
import android.webkit.*
import android.widget.ProgressBar

@SuppressLint("JavascriptInterface")
abstract class EasyWebview @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : RelativeLayout(context, attrs) {
    var webView: WebView? = null
    private var progressBar: ProgressBar? = null
    private val _2dp: Int
    private val mWebClient: MyWebViewClient

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun initView() {
        webView = WebView(context)
        this.addView(webView, LayoutParams(-1, -1))
        progressBar = ProgressBar(context, null, 16842872)
        progressBar!!.progressDrawable = this.resources.getDrawable(R.drawable.progressbar)
        progressBar!!.max = 100
        val progressP = LayoutParams(-1, 2 * _2dp)
        progressP.addRule(10)
        this.addView(progressBar, progressP)
    }

    private fun setWebCfg() {
        webView?.webViewClient = mWebClient
        webView?.webChromeClient = MyWebChromeClient()
    }

    open fun loadUrl(url: String?) {
        webView?.loadUrl(url!!)
    }

    fun loadData(data: String?, mimeType: String?, encoding: String?) {
        webView?.loadData(data!!, mimeType, encoding)
    }

    open fun postUrl(url: String?, bytes: ByteArray?) {
        webView?.postUrl(url!!, bytes!!)
    }

    fun canGoback(): Boolean {
        return webView?.canGoBack() == true
    }

    fun goBack() {
        if (canGoback()) {
            webView?.goBack()
        }
    }

    fun destory() {
        webView?.destroy()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        destory()
    }

    abstract fun onInit()
    fun addJavascriptInterface(obj: Any?, jsName: String?) {
        webView?.addJavascriptInterface(obj!!, jsName!!)
    }

    abstract fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean
    fun shouldInterceptRequest(
        webView: WebView?,
        webResourceRequest: WebResourceRequest
    ): WebResourceResponse? {
        return mWebClient.superShouldInterceptRequest(webView, webResourceRequest)
    }

    open fun shouldInterceptRequest(webView: WebView?, url: String?): WebResourceResponse? {
        return mWebClient.superShouldInterceptRequest(webView, url)
    }

    fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {}
    open fun onPageFinished(view: WebView?, url: String?) {}
    open fun onReceivedTitle(view: WebView?, title: String?) {}
    abstract fun onJsAlert(
        view: WebView?,
        url: String?,
        message: String?,
        result: JsResult?
    ): Boolean

    abstract fun onJsConfirm(
        view: WebView?,
        url: String?,
        message: String?,
        result: JsResult?
    ): Boolean

    abstract fun onJsPrompt(
        view: WebView?,
        url: String?,
        message: String?,
        defaultValue: String?,
        result: JsPromptResult?
    ): Boolean

    internal inner class MyWebChromeClient : WebChromeClient() {
        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
            this@EasyWebview.onReceivedTitle(view, title)
        }

        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            if (newProgress == 100) {
                progressBar!!.visibility = INVISIBLE
            } else {
                progressBar!!.progress = newProgress
            }
        }

        override fun onJsAlert(
            view: WebView,
            url: String,
            message: String,
            result: JsResult
        ): Boolean {
            return this@EasyWebview.onJsAlert(view, url, message, result)
        }

        override fun onJsConfirm(
            view: WebView,
            url: String,
            message: String,
            result: JsResult
        ): Boolean {
            return this@EasyWebview.onJsConfirm(view, url, message, result)
        }

        override fun onJsPrompt(
            view: WebView,
            url: String,
            message: String,
            defaultValue: String,
            result: JsPromptResult
        ): Boolean {
            return this@EasyWebview.onJsPrompt(view, url, message, defaultValue, result)
        }
    }

    internal inner class MyWebViewClient : WebViewClient() {
        override fun onReceivedSslError(
            webView: WebView,
            sslErrorHandler: SslErrorHandler,
            sslError: SslError
        ) {
            sslErrorHandler.proceed()
        }

        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            return this@EasyWebview.shouldOverrideUrlLoading(view, url)
        }

        override fun shouldInterceptRequest(
            webView: WebView,
            webResourceRequest: WebResourceRequest
        ): WebResourceResponse? {
            return this@EasyWebview.shouldInterceptRequest(webView, webResourceRequest)
        }

        @SuppressLint("NewApi")
        fun superShouldInterceptRequest(
            webView: WebView?,
            webResourceRequest: WebResourceRequest
        ): WebResourceResponse? {
            return if (VERSION.SDK_INT < 21) super.shouldInterceptRequest(
                webView,
                webResourceRequest.url.toString()
            ) else super.shouldInterceptRequest(webView, webResourceRequest)
        }

        override open fun shouldInterceptRequest(
            webView: WebView,
            url: String
        ): WebResourceResponse? {
            return this@EasyWebview.shouldInterceptRequest(webView, url)
        }

        fun superShouldInterceptRequest(webView: WebView?, s: String?): WebResourceResponse? {
            return super.shouldInterceptRequest(webView, s)
        }

        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
            progressBar!!.visibility = VISIBLE
            progressBar!!.progress = 0
            this@EasyWebview.onPageStarted(view, url, favicon)
        }

        override open fun onPageFinished(view: WebView, url: String) {
            super.onPageFinished(view, url)
            progressBar!!.visibility = INVISIBLE
            this@EasyWebview.onPageFinished(view, url)
        }

        override fun onFormResubmission(view: WebView, dontResend: Message, resend: Message) {
            dontResend.sendToTarget()
        }
    }

    companion object {
        private const val TAG = "EasyWebview"
    }

    init {
        mWebClient = MyWebViewClient()
        _2dp = dip2px(context, 2f)
        initView()
        setWebCfg()
        onInit()
    }
}